package ipebg.mzk.provider.system.service.impl;

import ipebg.mzk.facade.entity.TokenEntity;
import ipebg.mzk.facade.system.service.TokenService;
import ipebg.mzk.provider.system.dao.TokenDao;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

/**
 * Created by LT on 2016/4/18.
 */
@Service("tokenServiceImpl")
public class TokenServiceImpl implements TokenService {
    private TokenDao tokenDao;

    @Override
    public String  save(TokenEntity entity) {
        try {
			tokenDao.saveOrUpdate(entity);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return entity.getTokenId();
    }

    @Override
    public boolean isValid(String empNo, String token) {
        TokenEntity tokenEntity = tokenDao.findToken(empNo, token);
        if (tokenEntity == null) {
            return false;
        } else {
            return true;
        }
    }

    public TokenEntity addToken(String empNo) {
        TokenEntity tokenEntity = new TokenEntity();
        tokenEntity.setEmpNo(empNo);
        tokenEntity.setEnable(1);
        tokenEntity.setValidDt(DateUtils.addDays(new Date(), 3));
        try {
			tokenDao.saveOrUpdate(tokenEntity);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return tokenEntity;
    }

    @Resource(name="tokenDao")
    public void setTokenDao(TokenDao tokenDao) {
        this.tokenDao = tokenDao;
    }
}
